package main

import "fmt"

func main() {
	fmt.Println(combinationSum([]int{2, 3, 6}, 7))
}

func combinationSum(candidates []int, target int) [][]int {
	var res [][]int
	var list []int
	var helper func(target int, index int)
	helper = func(target int, index int) {
		if target == 0 {
			tmp := make([]int, len(list))
			copy(tmp, list)
			res = append(res, tmp)
			return
		} else if target < 0 {
			return
		}

		for i := index; i < len(candidates); i++ {
			list = append(list, candidates[i])
			helper(target-candidates[i], i)
			list = list[:len(list)-1]
		}
	}
	helper(target, 0)
	return res
}
